<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    button{
      position: absolute;
      z-index: 2;
    }
    #box {
      position: absolute;
      display: none;
      left: 50%;
      top: 50%;
      /* transform: translate(-50%, -50%); */
      z-index: 2;
      width: 300px;
      height: 230px;
      border: 1px solid #ccc;
      background-color: #fff;
    }
    #header {
      padding: 0 10px;
      background-color: pink;
      height: 50px;
      line-height: 50px;
      user-select: none;
    }
    #header span {
      float: right;
      cursor: pointer;
    }

    #mask {
      position: absolute;
      display: none;
      top: 0;
      left: 0;
      z-index: 1;
      width: 100%;
      min-height: 100%;
      background-color: rgba(0, 0, 0, 0.2);
    }
  </style>
</head>
<body>
  <div id="mask"></div>
  <button id="show">显示</button>
  <div id="box">
    <div id="header">标题<span>X</span></div>
    123132
  </div>

  <script>
    var mask = document.querySelector('#mask')
    var show = document.querySelector('#show')
    var box = document.querySelector('#box')
    var span = document.querySelector('span')

    show.onclick = function() {
      box.style.display = 'block'
      mask.style.display = 'block'
    }
    span.onclick = function() {
      box.style.display = 'none'
      mask.style.display = 'none'
    }

    box.onmousedown = function(e) {
      var x = e.pageX - this.offsetLeft  // x 鼠标按下距离盒子的左侧距离
      var y = e.pageY - this.offsetTop

      document.onmousemove = function(e) {
        box.style.left = e.pageX - x + 'px'  // 鼠标移动 保持这个位置
        box.style.top = e.pageY - y + 'px'
      }
    }

    document.onmouseup = function() {
      document.onmousemove = null
    }
  </script>
</body>
</html>